package com.until;

import com.entity.Emp;
import com.entity.OperLog;
import com.service.IOperLogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;

/**
 * TODO
 *
 * @version 1.0
 * @description:
 * @fileName:OperLogRecordUtil
 * @author:
 * @createTime: 2022-05-06  21:56:47
 */

@Component
@Aspect
public class OperLogRecordUtil {
    //切入点配置
    @Pointcut(value = "execution(* com.service.impl.*.add*(..)) || execution(* com.service.impl.*.insert*(..)) || execution(* com.service.impl.*.update*(..)) || execution(* com.service.impl.*.delete*(..))")
    public void pointCutA(){

    }

    @Resource
    private IOperLogService operLogService;

    @Resource
    private HttpSession session;

    @Resource
    private HttpServletRequest request;

    //日志记录方法
    @AfterReturning(pointcut = "pointCutA()")
    public void recordOperLog(JoinPoint joinPoint){
        //通过aop提供的Joinpoint获取到业务执行方法的名称
        String methodName=joinPoint.getSignature().getName();

        OperLog operLog=new OperLog();
        operLog.setTitle("图书分销系统");
        //设置操作日志类型   1 添加 2 更新  3删除
        int bzType=ConstantUtil.operLogBusinessType.INSERT;
        //判断拦截到方法是否以update开头
        if(methodName.startsWith("update")){
            bzType=ConstantUtil.operLogBusinessType.UPDATE;
        }else if(methodName.startsWith("del")){
            bzType=ConstantUtil.operLogBusinessType.DELETE;
        }
        operLog.setBusinessType(bzType);
        //获取执行类的名称  如果业务类是DeptServiceImpl 获得到就是com.service.impl.DeptServiceImpl
        //如果业务类是MenuServiceImpl  获取到的就是com.service.impl.MenuServiceImpl
        String targetName=joinPoint.getTarget().getClass().getName();
        operLog.setMethod(targetName+"."+methodName);
        operLog.setOperatorType(ConstantUtil.operLogOperatorType.BACKUSER);

        Emp emp=(Emp) session.getAttribute("empInfo");
        operLog.setOperName(emp.getEmpName());
        operLog.setDeptName("管理部");

        //通过request获取请求相关信息
        //获取url
        StringBuffer requestURL = request.getRequestURL();
        operLog.setOperUrl(requestURL.toString());
        //获取请求的IP地址
        String remoteAddr = request.getRemoteAddr();
        operLog.setOperIp(remoteAddr);

        operLog.setOperLocation("郑州");

        //获取业务方法参数
        //获取所有参数  数组
        Object[] args = joinPoint.getArgs();
        if(args!=null){
            //获取第一个参数，使用反射获取名称
            String firstParamName = args[0].getClass().getName();
            operLog.setOperParam(firstParamName);
        }
        operLog.setStatus(ConstantUtil.operStatus.SUCCESS);
        operLog.setErrorMsg("no error");
        operLog.setOperTime(new Date());
        operLogService.insert(operLog);
    }
}
